/*
输入打印行数n，打印如下字符图形到屏幕上和字符文件abc.txt中。
           1
        3  5
     7  9 11
 13 15 17 19
 */
import java.util.Scanner;
import java.io.*;

public class Demo04 {
    public static void main(String[] args) {
        System.out.print("请输入打印行数n：");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); //将用户输入存储到变量n中
        Print(n);
    }
    //输出到文件
    public static void WritoFile(String text, String FilePath) {
        try {
            File file = new File(FilePath); //根据路径得到文件
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            ps.println(text); //写入文件
        } catch (FileNotFoundException error) { //抛出异常
            error.printStackTrace();
        }
    }
    //打印字符图形函数
    public static void Print(int n) {
        int num = -1;
        String text = "";
        //输出到屏幕上
        for (int i = 1; i <= n; i++) {
            //输出前面的空格
            for (int j = 1; j <= n - i; j++) {
                System.out.print("\t");
                text += "\t";
            }
            //输出数字
            for (int j = 1; j <= i; j++) {
                num += 2;
                System.out.print(num + "\t");
                text = text + num + "\t";
            }
            System.out.println();
            text += "\n";
        }
        //输出到文件abc.txt中
        WritoFile(text, "C:\\Users\\98317\\Desktop\\JavaExerciseCode\\exci07\\abc.txt");
    }
}
